﻿using MediatR;
using Purchase.ReadAPI.APPlicant.Command;
using Purchase.Domain;
using ErrorCode;
using Purchase.Infrastructure.Interface;
using Microsoft.EntityFrameworkCore;
using Purchase.ReadAPI.DTOs.采购管理;
using AutoMapper;

namespace Purchase.ReadAPI.APPlicant.CommandHandler
{
    /// <summary>
    /// 物料清单显示命令执行
    /// </summary>
    public class GetBillOfMaterialCommandHandler : IRequestHandler<GetBillOfMaterialCommand, APIResult<List<BillOfMaterialDto>>>
    {
        private readonly IBaseRepository<BillOfMaterial> baseRepository;
        private readonly IMapper mapper;

        public GetBillOfMaterialCommandHandler(IBaseRepository<BillOfMaterial> baseRepository,IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<List<BillOfMaterialDto>>> Handle(GetBillOfMaterialCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<BillOfMaterialDto>> result = new APIResult<List<BillOfMaterialDto>>();
            var list = await baseRepository.GetAll().Result.Include(x=>x.Components).AsNoTracking().ToListAsync();
            result.Data = mapper.Map<List<BillOfMaterialDto>>(list);
            return await Task.FromResult(result);
        }
    }
}
